﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Interface;
using System.Web;
using System.Web.Security;
using LY.Common;
using LY.Common.Exceptions;
using LY.Entity.Entities;
using LY.Entity;

namespace LY.Extension
{
    public class MyContext
    {

        /// <summary>
        /// 当前登录账户的标识
        /// </summary>
        /// <value>The identity.</value>
        public static string Identity
        {
            get
            {

                string uid = HttpContext.Current.User.Identity.Name;
                if (string.IsNullOrEmpty(uid))
                {
                    if (AppConfig.IsDebugMode)
                    {
                        return AppConfig.Get("DebuggerUserId");
                    }
                    else
                    {
                        throw new NoAuthorizeExecption();
                    }
                }
                return uid;
            }
        }
        public static bool IsIsAuthenticated
        {
            get
            {
                if (AppConfig.IsDebugMode)
                {
                    string userid = AppConfig.Get("DebuggerUserId");
                    if (!HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        FormsAuthentication.SetAuthCookie(userid, false);
                    }
                    return true;
                }
                else
                {
                    return HttpContext.Current.User.Identity.IsAuthenticated;
                }
            }
        }
     

        /// <summary>
        /// 当前登录账户的完整信息
        /// </summary>
        /// <value>The current user.</value>
        public static U_Member CurrentLoginUser
        {
            get
            {
                if (HttpContext.Current.Session["CurrentUser"] != null)
                {
                    return ObjectToStrongType<U_Member>(HttpContext.Current.Session["CurrentUser"]);
                }
                else
                {
                    if (IsIsAuthenticated && !string.IsNullOrEmpty(Identity))
                    {
                        var cuo = CLoginService.GetUserById(Int32.Parse(Identity));
                        if (cuo != null)
                        {
                            return cuo;
                        }
                        return null;
                    }
                    return null;

                }
            }
        }
        /// <summary>
        /// 获取用户的IP
        /// </summary>
        /// <value>The user IP.</value>
        public static string UserIP
        {
            get
            {
                string usip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (String.IsNullOrEmpty(usip))
                    usip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                return usip;
            }
        }
      
        private static ILoginService _ILoginService;
        private static ILoginService CLoginService
        {
            get
            {
                if (_ILoginService == null)
                {
                    _ILoginService = ServiceFactory.GetInstance<ILoginService>();
                }
                return _ILoginService;
            }
        }

        /// <summary>
        /// 将object类型转换成指定类型，吞掉转换异常等情况
        /// </summary>
        /// <typeparam name="T">需要转换成的类型</typeparam>
        /// <param name="o">需要转换的对象</param>
        /// <returns></returns>
        private static T ObjectToStrongType<T>(object o) where T : class, new()
        {
            T t = null;
            if (o != null)
            {
                t = o as T;
            }
            return t;
        }

        
    }
}
